<?php
namespace App\Api\Appfc114;

use PhalApi\Api;
use PhalApi\Exception\BadRequestException;
use PhalApi\Request;
use Auth\Api\Token as Token;
use App\Domain\Appfc114\Customer as DomainCustomer;

ini_set("display_errors","off");
/**
 * 客户接口服务
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */

class Customer extends Api {

    /**
     * token对象
     */
    public $token;

    /**
     * 当前用户信息
     */
    public $user;

    public function __construct()
    {
        //允许所有域名发起的跨域请求
        header('Access-Control-Allow-Origin: *');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        //header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        //header('Access-Control-Allow-Headers:Authorization,content-type');  //响应头 请按照自己需求添加。
        //$this->di = \PhalApi\DI();
        $this->token = new Token();


    }

	public function getRules() {
        return array(
            //客户接口
            'getList' => array(),
            'add' => array(
                'name' 	=> array('name' => 'name', 'type'=>'string', 'require' => true, 'min' => 1, 'max' => '30', 'desc' => '客户姓名'),
                'mobile'  => array('name' => 'mobile', 'require' => true, 'regex' => "/^(13[0-9]|14[5|7|9]|15[0|1|2|3|5|6|7|8|9]|16[6]|17[0|1|2|3|5|6|7|8]|18[0-9]|19[8|9])\d{8}$/i", 'desc'=>'客户手机'),
            ),
            'update' => array(
                'customer_id' => array('name' => 'customer_id', 'type'=>'int', 'require' => true, 'desc' => '客户ID'),
                'name' 	=> array('name' => 'name', 'type'=>'string', 'require' => true, 'min' => 1, 'max' => '30', 'desc' => '客户姓名'),
                'mobile'  => array('name' => 'crm_mobile', 'require' => false, 'regex' => "/^(13[0-9]|14[5|7|9]|15[0|1|2|3|5|6|7|8|9]|16[6]|17[0|1|2|3|5|6|7|8]|18[0-9]|19[8|9])\d{8}$/i", 'desc'=>'客户手机'),
            ),
            'detail' => array(
                'customer_id' => array('name' => 'customer_id', 'type'=>'int', 'require' => true, 'desc' => '客户ID'),
            ),
            //到访接口
            "addVisit"=> array(
                'name' 	=> array('name' => 'name', 'type'=>'string', 'require' => true, 'min' => 1, 'max' => '30', 'desc' => '客户姓名'),
                'visit_type'  => array('name' => 'visit_type', 'require' => true, 'desc'=>'类型'),
                'type'  => array('name' => 'type', 'require' => true, 'desc'=>'数据类型类型(fc_esf,fc_project)'),
                'customer_id'  => array('name' => 'customer_id', 'require' => true, 'desc'=>'客户id'),
                'action_id'  => array('name' => 'action_id', 'require' => true, 'desc'=>'项目id')
            ),
            'addCustomerAndVisit'=> array(
                'name' 	=> array('name' => 'name', 'type'=>'string', 'require' => true, 'min' => 1, 'max' => '30', 'desc' => '客户姓名'),
                'mobile'  => array('name' => 'mobile', 'require' => true, 'regex' => "/^(13[0-9]|14[5|7|9]|15[0|1|2|3|5|6|7|8|9]|16[6]|17[0|1|2|3|5|6|7|8]|18[0-9]|19[8|9])\d{8}$/i", 'desc'=>'客户手机'),
                'visit_type'  => array('name' => 'visit_type', 'require' => true, 'desc'=>'类型'),
                'type'  => array('name' => 'type', 'require' => true, 'desc'=>'数据类型类型(fc_esf,fc_project)'),
            ),
            'listVisit' => array(),
            'detailVisit' => array(
                'visit_id' => array('name' => 'visit_id', 'type'=>'int', 'require' => true, 'desc' => '到访id'),
            ),
            'deleteVisit' => array(
                'visit_id' => array('name' => 'visit_id', 'type'=>'int', 'require' => true, 'desc' => '到访id'),
            )
        );

	}

    /**
     * 客户列表
     * @desc 返回客户列表
     * @return obj 客户信息
     */
    public function getList() {
        $this->user = $this->token->checkToken();

        $domain = new DomainCustomer();
        $rs = $domain->getList($this->user["user_id"]);
        return $rs;
    }


    /**
     * 新增客户
     * @desc 添加客户信息
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */

    public function add() {
        $this->user = $this->token->checkToken();

        error_log("addCustomer!", 0);
        //插入主表数据
        $domain = new DomainCustomer();
        $id = $domain->add($this->user["user_id"]);
        return $id;
    }

    /**
     * 更新意向用户
     * @desc 更新意向客户信息
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */
    public function update(){
        $this->user = $this->token->checkToken();
        error_log("updateCustomer!", 0);
        //获取参数
        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainCustomer();
        $id = $domain->update($params,$this->user["user_id"]);

        return $params["customer_id"];

    }

    /**
     * 获取客户详情
     * @desc 获取客户详情
     * @return array data 客户信息
     * @return int time 当前时间戳
     */
    public function detail()
    {
        $this->user = $this->token->checkToken();
        error_log("updateCustomer!", 0);
        //获取参数
        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainCustomer();
        $data = $domain->detail($params["customer_id"],$this->user["user_id"]);

        return $data;
    }

    /**
     * 新增到访
     * @desc 新增到访
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */

    public function addVisit() {
        $this->user = $this->token->checkToken();
        error_log("AddVisit!", 0);
        //插入主表数据
        $domain = new DomainCustomer();
        $id = $domain->addVisit($this->user["user_id"]);
        return $id;
    }

    /**
     * 新增客户并添加到访
     * @desc 添加客户同时新增到访
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */

    public function addCustomerAndVisit() {
        $this->user = $this->token->checkToken();

        error_log("addCustomerAndVisit!", 0);
        //新增客户并添加到访数据
        $domain = new DomainCustomer();
        $id = $domain->addCustomerAndVisit($this->user["user_id"]);
        return $id;
    }

    /**
     * 带看回访列表
     * @desc 返回带看列表
     * @return obj 带看回访数据
     */
    public function getVisitList() {
        $this->user = $this->token->checkToken();

        $domain = new DomainCustomer();
        $rs = $domain->getVisitList($this->user["user_id"]);
        return $rs;
    }

    /**
     * 到访详情
     * @desc 获取到访详情
     * @return array data 客户信息
     * @return int time 当前时间戳
     */
    public function detailVisit()
    {
        $this->user = $this->token->checkToken();
        error_log("DetailVisit!", 0);
        //获取参数
        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainCustomer();
        $data = $domain->detailVisit($params["visit_id"],$this->user["user_id"]);
        return $data;
    }

    /**
     * 删除到访
     * @desc 删除到访
     * @return int id 删除记录
     * @return int time 当前时间戳
     */
    public function deleteVisit(){
        $this->user = $this->token->checkToken();
        error_log("DeleteVisit!", 0);

        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainCustomer();
        $data = $domain->deleteVisit($params["visit_id"]);
        return $data;
    }

}
